package com.hcinfos.gsoo.ui;

import java.util.Random;

import com.hcinfos.gsoo.R;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

/**
 * 幸运大转盘
 * 
 * @author john
 * 
 */
public class LuckyActivity extends Activity {

	private ImageView indicator_iv;
	public int maxPrizeVal = 999999999;
	public int currentPrize = 0;

	private SoundPool soundPool = null;
	private int explosionId = 0; // 音频-内存加载ID
	private int playSourceId = 0; // 音频-播放ID

	private Prize[] prizes = new Prize[] {
			new Prize(Prize.TYPE_IP5S, 1, 30f, 1, "奖励IPhone5s${value}台"),
			new Prize(Prize.TYPE_RPVAL, (long) (maxPrizeVal * 0.0001), 90f,
					5000, "奖励RP值${value}点"),
			new Prize(Prize.TYPE_RPVAL, (long) (maxPrizeVal * 0.5999), 150f,
					20, "奖励RP值${value}点"),
			new Prize(Prize.TYPE_RPVAL, (long) (maxPrizeVal * 0.25), 210f, 50,
					"奖励RP值${value}点"),
			new Prize(Prize.TYPE_RPVAL, (long) (maxPrizeVal * 0.1), 270f, 200,
					"奖励RP值${value}点"),
			new Prize(Prize.TYPE_RPVAL, (long) (maxPrizeVal * 0.05), 330f, 500,
					"奖励RP值${value}点") };

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lucky);
		this.currentPrize = 0;
		// 计算奖品
		for (Prize prize : prizes) {
			prize.setStartPrizeVal(this.currentPrize);
			this.currentPrize += prize.getOdds();
			prize.setEndPrizeVal(this.currentPrize);
		}

		indicator_iv = (ImageView) findViewById(R.id.indicator_iv);
		indicator_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doStart();
			}
		});

	}

	@Override
	protected void onResume() {

		super.onResume();
		if (soundPool == null) {
			// 指定声音池的最大音频流数目为10，声音品质为5
			soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
			// 载入音频流，返回在池中的id
			explosionId = soundPool.load(this, R.raw.music, 1);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (soundPool != null) {
			soundPool.stop(explosionId);
			soundPool.release();
			soundPool = null;
		}
	}

	private void doStart() {
		playSourceId = soundPool.play(explosionId, 1, 1, 0, -1, 1);
		// 计算中奖
		long thePrizeVal = Math.abs(new Random().nextInt(currentPrize));

		Prize goodPrize = null;
		// 计算奖品
		for (Prize prize : prizes) {
			if (thePrizeVal >= prize.getStartPrizeVal()
					&& thePrizeVal < prize.getEndPrizeVal()) {
				goodPrize = prize;
				break;
			}
		}

		if (null == goodPrize) {
			goodPrize = prizes[2];
		}
		final Prize finalGoodPrize = goodPrize;
		AnimationSet animSet = new AnimationSet(true);
		// 加速
		RotateAnimation accelAnima = new RotateAnimation(0f, 359,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		accelAnima.setDuration(200);
		accelAnima.setRepeatCount(6);
		accelAnima.setInterpolator(new AccelerateInterpolator());
		// animSet.addAnimation(accelAnima);

		RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 360f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateAnimation.setDuration(500);
		rotateAnimation.setRepeatCount(5);
		rotateAnimation.setInterpolator(new LinearInterpolator());
		// animSet.addAnimation(rotateAnimation);

		// 减速
		RotateAnimation decelAnima = new RotateAnimation(0f,
				3600f + finalGoodPrize.getAngle(), Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		decelAnima.setDuration(5000);
		decelAnima.setRepeatCount(0);
		decelAnima.setInterpolator(new DecelerateInterpolator());
		animSet.addAnimation(decelAnima);
		animSet.setFillAfter(true);

		animSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				soundPool.stop(playSourceId);
				String finalRp = finalGoodPrize.getDes().replace("${value}",
						Float.toString(finalGoodPrize.getValue()));
				Log.d("wxf", "des: " + finalRp);
			}
		});
		this.indicator_iv.startAnimation(animSet);

	}

	public static class Prize {
		public long getStartPrizeVal() {
			return startPrizeVal;
		}

		public void setStartPrizeVal(long startPrizeVal) {
			this.startPrizeVal = startPrizeVal;
		}

		public long getEndPrizeVal() {
			return endPrizeVal;
		}

		public void setEndPrizeVal(long endPrizeVal) {
			this.endPrizeVal = endPrizeVal;
		}

		public String getDes() {
			return des;
		}

		public void setDes(String des) {
			this.des = des;
		}

		public int getType() {
			return type;
		}

		public void setType(int type) {
			this.type = type;
		}

		public long getOdds() {
			return odds;
		}

		public void setOdds(long odds) {
			this.odds = odds;
		}

		public float getAngle() {
			return angle;
		}

		public void setAngle(float angle) {
			this.angle = angle;
		}

		public float getValue() {
			return value;
		}

		public void setValue(float value) {
			this.value = value;
		}

		public static final int TYPE_IP5S = 0;
		public static final int TYPE_RPVAL = 1;
		private int type;// 类型
		private long odds;// 几率
		private long startPrizeVal;
		private long endPrizeVal;

		private float angle;// 角度
		private float value;// 数量
		private String des;// 描述

		public Prize(int type, long odds, float angle, float value, String des) {
			super();
			this.type = type;
			this.odds = odds;
			this.angle = angle;
			this.value = value;
			this.des = des;
		}
	}

}
